package command

import (
	"gitee.com/moyrn/sa/engine"
	"gitee.com/moyrn/sa/store"
	"gitee.com/moyrn/sa/store/models"
	"github.com/spf13/cobra"
	"time"
)

var rootCmd = &cobra.Command{
	Use:   "",
	Short: "",
	Long:  "",
}

func init() {
	rootCmd.Flags().StringVarP(&dbName, "dbName", "d", "store.db", "请输入数据库文件名")
	rootCmd.AddCommand(
		OutputCmd,
		runCmd,
	)
}

var (
	e     *engine.Engine
	seeds []models.Task
)

// 创建存储
func NewStore() (*store.Store, error) {
	return store.NewStore(dbName)
}

// 初始化引擎
/*
	@Params
	s			存储
	logPath 	日志路径
	threadNum	线程数(默认1)
*/
func InitEngine(s *store.Store, logPath string, threadNum ...int) error {
	var err error
	e, err = engine.NewEngine(s, logPath, time.Duration(tick)*time.Millisecond, threadNum...)
	if err != nil {
		return err
	}
	return nil
}

// 注册适配器
/*
	@Params
	adapters 适配器
*/
func RegisterAdapters(adapters []engine.Adapter) {
	e.RegisterAdapters(adapters)
}

// 执行
/*
	命令:(详情请运行 加参数 --help)
	run		执行程序
	output	导出文件

	@Params
	ts 初始任务
*/
func Run(ts ...models.Task) {
	seeds = ts
	if err := rootCmd.Execute(); err != nil {
		store.Log.Panic("Execute", err)
	}
}
